package com.example.demo.controller;


import com.example.demo.dao.SysUserDao;
import com.example.demo.entity.SysRole;
import com.example.demo.entity.SysUser;
import com.example.demo.service.JwtUserDetailService;
import com.example.demo.utils.JwtTokenUtil;
import com.example.demo.utils.JwtUserDetails;
import lombok.extern.log4j.Log4j;
import lombok.extern.log4j.Log4j2;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.http.MediaType;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;
import java.util.*;

/**
 * @author yangjj
 * @version V1.0
 * @date
 */
@RestController
@RequestMapping("/auth")
@Log4j2
public class AuthController {



    @Autowired
    SysUserDao sysUserDao;
    @Autowired
    AuthenticationManager authenticationManager;
    @Autowired
    JwtUserDetailService jwtUserDetailService;
    @Autowired
    JwtTokenUtil jwtTokenUtil;

    /**
     *  登陆成功
     *  @CacheEvict 删除缓存 value=缓存名称 key=键
     */
    @RequestMapping(value = "/login")
    @CacheEvict(value = "auth" ,key = "#user.userName")
    public String login(@RequestBody SysUser user) {
        try {
            log.info("登录参数userName:{},userPwd:{}",user.getUserName(),user.getUserPwd());
            SysUser sysUser = sysUserDao.findSysUserByUserName(user.getUserName());
            Collection<GrantedAuthority> grantedAuthorities = new ArrayList<>();
            for (SysRole sysRole : sysUser.getRoles()){
                GrantedAuthority authority = new SimpleGrantedAuthority(sysRole.getId().toString());
                grantedAuthorities.add(authority);
            }
            UsernamePasswordAuthenticationToken upToken = new UsernamePasswordAuthenticationToken(user.getUserName(), user.getUserPwd(), grantedAuthorities);
            final Authentication authentication = authenticationManager.authenticate(upToken);
            SecurityContextHolder.getContext().setAuthentication(authentication);
            final JwtUserDetails userDetails = (JwtUserDetails) jwtUserDetailService.loadUserByUsername(user.getUserName());
            final String token = jwtTokenUtil.generateToken(userDetails);
            log.info("token:{}",token);
            return token;
        }catch (Exception e){
            log.error("登陆异常"+e.getMessage(),e);
        }
        return "登陆失败";
    }

    @RequestMapping(value = "/test")
    public String test(@RequestBody SysUser user) {
        log.info("测试是否有权限");
        return "有权限";
    }

}	

